package com.zh.chat.gui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import com.zh.chat.controller.ClientContext;
import com.zh.chat.entity.UserStatus;

@SuppressWarnings("serial")
public class MainFrameStatusPopBtn extends JLabel{
	private ClientContext clientContext = null;
	private int status;
	
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
		updateView();
	}
	public void setClientContext(ClientContext clientContext) {
		this.clientContext = clientContext;
	}
	public MainFrameStatusPopBtn() {
		super("正在获取状态...");
		final JPopupMenu menu=new JPopupMenu();//创建弹出菜单
		Icon onlineIcon = new ImageIcon("src/com/zh/chat/image/online.png");
		JMenuItem onlineItem = new JMenuItem("在线",onlineIcon);//设置状态图标和文字
		onlineItem.addMouseListener(new MouseAdapter() {//绑定按钮的事件
			@Override
			/**
			 * 1启动线程<br/>
			 * 2发送请求<br/>
			 * 3接收返回的信息<br/>
			 * 4根据返回信息更换状态图标<br/>
			 * 5刷新界面时图标生效
			 */
			public void mouseReleased(MouseEvent e) {
				clientContext.changeStatus(UserStatus.ONLINE);
			}
		});
		Icon busyIcon = new ImageIcon("src/com/zh/chat/image/busy.png");
		JMenuItem busyItem = new JMenuItem("忙碌",busyIcon);
		busyItem.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				clientContext.changeStatus(UserStatus.BUSY);
			}
		});
		Icon awayIcon = new ImageIcon("src/com/zh/chat/image/leave.png");
		JMenuItem awayItem = new JMenuItem("离开",awayIcon);
		awayItem.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				clientContext.changeStatus(UserStatus.AWAY);
			}
		});
		Icon hideIcon = new ImageIcon("src/com/zh/chat/image/hide.png");
		JMenuItem hideItem = new JMenuItem("隐身",hideIcon);
		hideItem.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				clientContext.changeStatus(UserStatus.HIDE);
			}
		});
		Icon offlineIcon = new ImageIcon("src/com/zh/chat/image/offline.png");
		JMenuItem offlineItem = new JMenuItem("离线",offlineIcon);
		offlineItem.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				clientContext.changeStatus(UserStatus.OUTLINE);
			}
		});
		menu.add(onlineItem);  
		menu.add(busyItem);  
		menu.add(awayItem);  
		menu.add(hideItem);
		menu.add(offlineItem);
		add(menu);
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				menu.show(MainFrameStatusPopBtn.this, 0, 18);
			}
		});
	}
	/**
	 * 更新按钮视图
	 */
	private void updateView() {
		String statusText=null;//文字
		Icon icon = null;//按钮图标
		switch (status) {
		case UserStatus.ONLINE:
			statusText = "在线";
			icon = new ImageIcon("src/com/zh/chat/image/online.png");
			break;
		case UserStatus.BUSY:
			statusText = "忙碌";
			icon = new ImageIcon("src/com/zh/chat/image/busy.png");
			break;
		case UserStatus.AWAY:
			statusText = "离开";
			icon = new ImageIcon("src/com/zh/chat/image/leave.png");
			break;
		case UserStatus.HIDE:
			statusText = "隐身";
			icon = new ImageIcon("src/com/zh/chat/image/hide.png");
			break;
		case UserStatus.OUTLINE:
			statusText = "离线";
			icon = new ImageIcon("src/com/zh/chat/image/offline.png");
			break;
		default:
			break;
		}
		this.setIcon(icon);
		if(status==1||status==5){//不显示右下角小图标
			clientContext.getMainFrame().setSmallIcon(null);
			if(status==5){//如果是离线就换成黑白图片
				clientContext.getMainFrame().setOfflineIcon();
			}
		}else{//显示右下角小图标
			clientContext.getMainFrame().setSmallIcon(icon);
		}
		this.setText(statusText + " ▽");
	}
}
